package servlet.classes;

import com.alibaba.fastjson.JSONObject;
import utils.JdbcUtil;
import utils.ResultData;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 *      类别信息删除
 */
@WebServlet(urlPatterns = "/classDelete")
public class ClassDelete extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        JdbcUtil jdbc = new JdbcUtil();
        Connection connection = jdbc.getConnection();
        Integer id = Integer.valueOf(req.getParameter("id"));

        String sql = "delete from classes_table where id=?";

        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,id);

            int i = preparedStatement.executeUpdate();
            ResultData resultData = null;

            if (i>0){
                resultData = ResultData.success();
            }else {
                resultData = ResultData.fail();
            }

            Object o = JSONObject.toJSON(resultData);
            resp.getWriter().println(o.toString());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
